import Vue from 'vue'
import VueRouter from 'vue-router'
import login from "../components/login.vue"
import home from "../components/home.vue"
import welcome from "../components/welcome.vue"
import user from "../components/Users/user.vue"
import right from "../components/Rights/right.vue"
import role from "../components/Rights/role.vue"
import categories from "../components/Goods/goods_cate.vue"
import params from "../components/Goods/goods_params.vue"
import list from "../components/Goods/goods_list.vue"
import addgoods from "../components/Goods/addgoods.vue"
import order from "../components/Order/order.vue"
import report from "../components/Report/report.vue"
Vue.use(VueRouter)



const router = new VueRouter({
  routes:[
    { path: '/', redirect: "/login" },
    { path: '/login', component: login },
    { path: "/home",  component: home ,redirect:"/welcome",children:[
    { path:"/welcome",component:welcome},
    { path:"/users",component: user },
    { path:"/rights",component:right},
    { path:"/roles",component:role},
    { path:"/categories",component:categories},
    { path:"/params",component:params},
    { path:"/goods",component:list},
    { path:"/goods/add",component:addgoods},
    {path:"/orders",component:order},
    {path:"/reports",component:report}

      
    ]},
  ]
})









// 挂载路由导航守卫  避免无聊的人直接访问授权的页面
router.beforeEach((to,from,next)=>{
  // to是将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行  或者强制跳转  next("/login")
  if(to.path==="/login") {
    return  next()//直接放行
  }
  // 如果不是访问的登录页面，那么先从本地存储内获得token的值，如果没有则强制跳转到登录的页面
  const tokenStr=window.sessionStorage.getItem("token")
  if(!tokenStr){
    return next("/login");
  }
  next()
})

export default router
